package com.hex.ds.hdrs.app.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.app.dto.AppUserDto;
import com.hex.ds.hdrs.app.service.impl.AppUserService;
import com.hex.ds.hdrs.app.vo.AppUserVo;
import com.hex.ds.hdrs.common.page.PageData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/appUser")
public class AppUserController {
    @Value("${resetPwd}")
    private String resetPwd;
    @Value("${newUserPwd}")
    private String newUserPwd;
    @Value("${rsa.private_key}")
    private String privateKey;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AppUserService appUserService;

    @ApiOperation("用户分页查询")
    @GetMapping("/selectAppUser")
    public ResponseEntity<Object> selectAppUser(AppUserVo appUserVo, Page page) {
        log.info("【进入了AppUserController的selectAppUser】user={}", appUserVo);
        PageData pageData = new PageData(this.appUserService.selectAppUser(appUserVo, page, appUserVo.getAppCode()), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("获取所有用户")
    @ApiOperation("获取所有用户")
    @GetMapping(value = "/selectAllUser")
    public ResponseEntity<Object> selectAllUser(@RequestParam String appCode) {
        log.info("【进入了AppUserController.selectAllUser】");
        return new ResponseEntity(this.appUserService.selectAllUser(appCode), HttpStatus.OK);
    }

    @ApiOperation("/用户添加")
    @RequestMapping("/appAdd")
    public ResponseEntity<Object> add(@RequestBody AppUserDto user) {
        log.info("【进入了AppUserController的add】user={}", user);
        user.setPassword(this.passwordEncoder.encode(this.newUserPwd));
        this.appUserService.add(user);
        return new ResponseEntity("添加成功", HttpStatus.OK);
    }

    @ApiOperation("/获取某个用户信息")
    @GetMapping("/getAppUserRole")
    public ResponseEntity<Object> getAppUserRole(@RequestParam String username, @RequestParam String appCode) {
        log.info("【进入了AppUserController.getAppUserRole】username={}", username);
        return new ResponseEntity(this.appUserService.getAppUserRole(username, appCode), HttpStatus.OK);
    }

    /*
     * @Method: getCrunUserAppPermission <br>
     * @Param: [appCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：获取当前用户在指定应用中的权限标识<br>
     * @Author： wz.li<br>
     * @Date： 2023/6/1 13:55 <br>
     * @Version： V2.0.2<br>
     */
    @ApiOperation("/获取当前用户指定应用的权限标识")
    @GetMapping("/getCrunUserAppPermission/{appCode:.+}")
    public ResponseEntity<Object> getCrunUserAppPermission(@PathVariable("appCode") String appCode) {
        log.debug("【进入了AppUserController.getCrunUserAppPermission】参数={}", appCode);
        return new ResponseEntity(appUserService.getCrunUserAppPermission(appCode), HttpStatus.OK);
    }

    @ApiOperation("/用户修改")
    @PutMapping("/appEdit")
    public ResponseEntity<Object> edit(@RequestBody AppUserDto user) {
        log.info("进入【AppUserController.add】user={}", user);
        this.appUserService.appEdit(user);
        return new ResponseEntity("用户修改", HttpStatus.OK);
    }

    @Log("删除用户")
    @ApiOperation("删除用户")
    @DeleteMapping(value = "/delete/{appCode:.+}")
    public ResponseEntity<Object> delete(@RequestBody List<String> username, @PathVariable("appCode") String appCode) {
        log.info("进入【AppUserController.delete】usernameList ={}", username.toString());
        this.appUserService.delete(username, appCode);
        return new ResponseEntity("删除用户成功", HttpStatus.OK);
    }

    @ApiOperation("/获取某个用户信息")
    @RequestMapping("/findUserByUserName")
    public ResponseEntity<Object> findUserByUserName(@RequestParam String username) {
        log.info("【进入了AppUserController.findUserByUserName】username={}", username);
        return new ResponseEntity(this.appUserService.findUserByUserName(username), HttpStatus.OK);
    }
}
